package com.resou.reader.bookstore.rank.fragment;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.SimpleLoadMoreView;
import com.resou.reader.R;
import com.resou.reader.api.entry.RankTypeBean;
import com.resou.reader.api.entry.TypeResultBean;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.bookstore.rank.adapter.RankContentAdapter;
import com.resou.reader.bookstore.rank.adapter.RankTypeAdadpter;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.view.Loading;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:  排行榜碎片
 * CreateTime: 2019-04 -09
 **/
public class RankBoyFragment extends ResouBaseFragment<RankFragmentPresenter> implements IRankFragmentContract.IRankFragmentView, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.left_recyclerView)
    RecyclerView mLeftRecyclerView;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRightRecyclerView;
    String rankType = "";
    private RankTypeAdadpter mRankTypeAdapter;
    private RankContentAdapter mRankContentAdapter;
    private LinearLayoutManager mLayoutManager;
    int page = 0;

    public RankBoyFragment() {

    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_rank_layout;
    }

    @Override
    protected void initView() {
        mLeftRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRankTypeAdapter = new RankTypeAdadpter(R.layout.item_rank_left_text, null);
        mLeftRecyclerView.setAdapter(mRankTypeAdapter);
        mRankTypeAdapter.setOnItemChildClickListener(this);
        mRankContentAdapter = new RankContentAdapter(R.layout.item_rank_right_layout, null);
        mSwipeRefreshLayout.setEnableScrollContentWhenLoaded(true);
//        mSwipeRefreshLayout.setEnablePureScrollMode(true);
        mLayoutManager = new LinearLayoutManager(getContext());
        mRightRecyclerView.setLayoutManager(mLayoutManager);
        mRightRecyclerView.setAdapter(mRankContentAdapter);
        if (mPresenter == null) {
            initPresenter();
        }
        mPresenter.quearTypeLabel("1");
        mSwipeRefreshLayout.setEnableLoadMore(false);
        mRankContentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                TypeResultBean.BookListBean list = (TypeResultBean.BookListBean) adapter.getData().get(position);
                BookDetailActivity.startDetailActivity(getActivity(),
                        list.getBookId());
            }
        });
        mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 0;
                mPresenter.quearContent(rankType, page);
            }
        });
        mSwipeRefreshLayout.setEnableClipFooterWhenFixedBehind(false);
        mRankContentAdapter.setPreLoadNumber(4);
        mRankContentAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                mPresenter.quearContent(rankType, page);
            }
        }, mRightRecyclerView);
    }

    @Override
    protected void retryRefreshData() {
        page = 0;
        mPresenter.quearContent(rankType, page);
    }


    @Override
    protected void initPresenter() {
        mPresenter = new RankFragmentPresenter(this);
    }

    @Override
    public void initData() {
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        if (!NetworkUtils.isNetworkConnected(getActivity())) {
            ToastUtil.makeShortToast("网络异常");
            if (page == 0 && mRankTypeAdapter.getData().size() == 0) {
                showNetErrorView(R.id.rlContent);
            }
        } else {
            ToastUtil.makeShortToast("数据异常，请稍后重试");
        }
//        mRankContentAdapter.setNewData(null);
    }

    @Override
    public void showEmpty() {

    }


    @Override
    public void setPageState(int gender) {
        if (mPresenter == null) {
            initPresenter();
        }
        page = 0;

    }

    @Override
    public void showTypeLabel(List<RankTypeBean> list) {
        if (page == 0) {
            list.get(0).setCheck(true);
            rankType = list.get(0).getRankType() + "";
            mPresenter.quearContent(list.get(0).getRankType() + "", page);
        }
        mRankTypeAdapter.setNewData(list);
    }

    @Override
    public void showContent(List<TypeResultBean.BookListBean> list) {
        if (page == 0) {
            mRankContentAdapter.setNewData(list);
        } else {
            mRankContentAdapter.addData(list);
        }
    }

    @Override
    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSwipeRefreshLayout;
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return mRankContentAdapter;
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        List<RankTypeBean> list = adapter.getData();
        rankType = list.get(position).getRankType() + "";
        updataTypeLabel(list, position);
        page = 0;
        mPresenter.quearContent(rankType, page);
        mRightRecyclerView.smoothScrollToPosition(0);
    }

    /**
     * 更新分类
     *
     * @param list
     * @param index
     */
    private void updataTypeLabel(List<RankTypeBean> list, int index) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isCheck()) {
                list.get(i).setCheck(false);
            }
        }
        list.get(index).setCheck(true);
        mRightRecyclerView.smoothScrollToPosition(0);
        mRankTypeAdapter.setNewData(list);
    }
}
